package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class Address {

    private int addressNo;
    private String streetName;
    private String streetNo;
    private String state;
    private City city;
    private Country country;

    /**
     *
     */
    public Address() {
    }

    /**
     *
     * @param addressNo
     */
    public Address(int addressNo) {
        this.addressNo = addressNo;
        String zip = "";
        String countryCode = "";
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM address WHERE addressNo=" + addressNo);
            while (rs.next()) {
                streetName = rs.getString("streetName");
                streetNo = rs.getString("streetNo");
                state = rs.getString("state");
                zip = rs.getString("zip");
                countryCode = rs.getString("countryCode");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Address", "There was an error performing the sql inquery\n").showException(ex);
        }
        city = new City(zip);
        country = new Country(countryCode);
    }

    /**
     *
     * @return
     */
    public int getAddressNo() {
        return addressNo;
    }

    public ArrayList<Address> getAddresses() {
        ArrayList<Address> addresses = new ArrayList<Address>();
        ArrayList<Integer> addressNos = new ArrayList<Integer>();
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM address ORDER BY addressNo");
            while(rs.next()){
                addressNos.add(rs.getInt("addressNo"));
            }
            for(int i = 0; i < addressNos.size(); i++){
                addresses.add(new Address(addressNos.get(i)));
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Address.getAddresses", "There was an error performing the sql inquery\n").showException(ex);
        }
        return addresses;
    }

    /**
     *
     * @param addressNo
     */
    public void setAddressNo(int addressNo) {
        this.addressNo = addressNo;
    }

    /**
     *
     * @param city
     */
    public void setCity(City city) {
        this.city = city;
    }

    /**
     *
     * @param country
     */
    public void setCountry(Country country) {
        this.country = country;
    }

    /**
     *
     * @param state
     */
    public void setState(String state) {
        this.state = state;
    }

    /**
     *
     * @param streetName
     */
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    /**
     *
     * @param streetNo
     */
    public void setStreetNo(String streetNo) {
        this.streetNo = streetNo;
    }

    @Override
    public String toString(){
        String addressStr = streetName + " " + streetNo + " " + city.getZip() + " " + state;
        return addressStr;
    }

    /**
     *
     */
    public void insert() {
        try {
            addressNo = DbCon.getInstance().insertAutoKey("INSERT INTO address(streetName, streetNo, zip, state, countryCode) VALUES ('" + streetName + "', '" + streetNo + "', '" + city.getZip() + "', '" + state + "', '" + country.getCountryCode() + "')");
        } catch (SQLException ex) {
            new EventMessage("SQL error in Address.insert()", "There was an error performing the sql inquery\n").showException(ex);
        }
    }
}
